<?php
//------------------------
// 商品规格模型
//-------------------------
namespace app\common\model;

use think\Model;
use think\Db;

class GoodsSpec extends Model{

	public function getSpec($goods_id){
		//商品规格 价钱 库存表 找出 所有 规格项id
        $keys = $this->where("goods_id", $goods_id)->value("GROUP_CONCAT(`key` SEPARATOR '_') ");

        $filter_spec = array();
        if ($keys) {
            // $specImage = M('SpecImage')->where(['goods_id'=>$goods_id,'src'=>['<>','']])->getField("spec_image_id,src");// 规格对应的 图片表， 例如颜色
            $keys = str_replace('_', ',', $keys);

            $filter_spec2 = Db::table('platform_type_spec')
			->join('platform_spec_item','platform_spec_item.spec_id = platform_type_spec.id')
			->where('platform_spec_item.id','in',$keys)
			->field('platform_spec_item.*,platform_type_spec.spec_name,platform_type_spec.seq')
			->order('platform_spec_item.id')
			->select()->toArray();

            foreach ($filter_spec2 as $key => $val) {
                $filter_spec[$val['spec_name']][] = array(
                    'item_id' => $val['id'],
                    'item' => $val['item'],
                );
            }
        }

        return $filter_spec;
	}

    //获取 规格 对应 价格 库存
    public function getGoodsSpecPrice($goods_id){
        $data = $this->where("goods_id", $goods_id)->field('key,price,store_count')->select()->toArray();
        $_data = array();
        foreach ($data as $key => $value) {
            $_data[$value['key']] = $value;
        }

        return json_encode($_data,true);
    }


}